<?php
require("../../../libraries/config.php");
require("../../../libraries/dbclass2.php");
require("../../../libraries/function.php");
require("../../../libraries/anime/function2.php");
require("../../../libraries/cache.php");
//Xcrud remove event ----------------
function xcrud_remove_post($id){
	$db=new db;
	$db->connect();
	$db->query('select * from index_post where id='.$id);
	$rc=$db->fetchOne();
		$pid=$rc['post_id'];
	if($rc['is_image']==1){
	
		$db->nonquery('delete from images where post_id='.$pid);
		$db->nonquery('delete from post_image where id='.$pid);
	}else
	if($rc['is_video']==1){
		$db->nonquery('delete from post_media where id='.$pid);
	}else
	if($rc['is_text']==1){
		$db->nonquery('delete from post_text where id='.$pid);
	}
	$db->close();


}

function xcrud_remove_post_image($id){
	$db=new db;
	$db->connect();
	

	
	$db->nonquery('delete from images where post_id='.$id);
	$db->nonquery('delete from index_post where post_id='.$id.' and is_image=1');
	$db->close();



}
function xcrud_remove_post_video($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from index_post where is_video=1 and post_id='.$id);
	$db->close();



}
function xcrud_remove_post_text($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from index_post where is_text=1 and post_id='.$id);
	$db->close();



}

// End Xcrud remove event ----------------

//Xcrud -update event
function xcrud_update_post_text($data,$primary,$xcrud){
	
	$data->set('alias',khongdau($data->get('title')));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".$data->get('cat_id').",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_text=1";
	$db->query($sql);
	$db->close();
	
	$cache=new cache;
	$cache->flush($primary,'post/post_text');
	
}

function xcrud_update_post_video($data,$primary,$xcrud){
	
	$data->set('alias',khongdau($data->get('title')));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".intval($data->get('cat_id')).",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_video=1";
	$db->query($sql);
	$db->close();
	$cache=new cache;
	$cache->flush($primary,'post/post_media');
	
}

function xcrud_update_post_image($data,$primary,$xcrud){

	$data->set('alias',khongdau($data['post_image.title']));
	
	$db=new db;
	$db->connect();
	if($data->get('sub_id')){
		$sub_id=intval($data->get('sub_id'));
	}else{
		$sub_id='NULL';
	}
	if($data->get('album_id')){
		$album_id=intval($data->get('album_id'));
	}else{
		$album_id='NULL';
	}
	$sql="update index_post set title='".mysql_real_escape_string($data->get('title'))."',cat_id=".intval($data->get('cat_id')).",sub_id=".$sub_id.",album_id=".$album_id." where post_id=".$primary." and is_image=1";
	$db->query($sql);
	$db->close();
	$cache=new cache;
	$cache->flush($primary,'post/post_media');
	
}
//End of xcrud -update event

//Xcrud truyen event ---------------------

//Xcrud check the loai -----
function check_anime_update($data,$primary,$xcrud){
	
	//Check user --------------
	$r1="";
	if($_SESSION['user']['level']!=1&& $_SESSION['user']['level']!=2&& $_SESSION['user']['level']!=5){
			//Chỉ còn lại user level 4
	
		$db=new db;
		$db->connect();
		$db->query("select upload_user,user_add,upload_user_add,theloai from anime where id=".intval($primary));
		$r1=$db->fetchOne();
		$db->close();
		
		if($_SESSION['user']['username']!=$r1['upload_user']
		&&$_SESSION['user']['username']!=$r1['user_add']
		&& strpos($r1['upload_user_add'],$_SESSION['user']['username'])===false){
			$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Anime này!');
		}
	}
	//-------------------------
//	echo $data->get('theloai');
//The loai dau ma khac the loai truoc thi moi goi ham ra//

	if($r1['theloai']!=$data->get('theloai')){
	$db=new db;
	$db->connect();
	$db->nonquery("delete from anime_theloai_fk where anime_id=".$primary);	
	$db->close();
	anime_theloai($primary,$data->get('theloai'));
	}
	$data->set('alias',khongdau(trim($data->get('name'))));

	
	//Clear cache truyen
	$cache =new cache;
	$cache->flush("anime".$primary,'anime');
	
}

function check_anime_insert($data,$xcrud){
	
   $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/
	$db=new db;
	$db->connect();
	$db->query("select id from anime where name = '".trim($data->get('name'))."'");
	if($db->num_rows()>0){
	 	$xcrud->set_exception('id','Manga này đã tồn tại rồi, xin vui lòng kiểm tra lại. Nếu  cần hỗ trợ xin vui lòng liên hệ ban quản trị Anime24h.vn!');
		$db->close();
	}else{
		$db->close();
		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
	//	$data->set('theloai',anime_theloai2($data->get('theloai')));
	}
	
	
}
function check_anime_afterinsert($data,$primary,$xcrud){
	
  // $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/

		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
		anime_theloai($primary,$data->get('theloai'));

	
	
}


// Xcrud end tags
function check_image($data){
$data = mb_convert_encoding($data, 'HTML-ENTITIES', "UTF-8"); 
$dom = new DOMDocument;
 $dom->loadHTML($data); 
 $sxe = simplexml_import_dom($dom);

	$j=0;	
	 foreach ($sxe->xpath('//img[@src]') as $a)
	 {	
		
		$j++;
	 }
 return $j;
 }
 //---------------------------------Anime 
  function anime_remove($id){
	  	$cache =new cache;
	$cache->flush("anime".$id,'anime');
 	$db=new db;
	$db->connect();
	$db->nonquery("delete from chapterk2 where manga=".$id);
	$db->nonquery("delete from anime_theloai_fk where anime_id=".$id);
	$db->nonquery("delete from anime_tag3 where anime_id=".$id);
	$db->nonquery("delete from anime_tag2 where anime_id=".$id);
	$db->nonquery("delete from anime_tag1_fk where anime_id=".$id);
	$db->close();
 }
 
 function theloai_remove($id){
 	$db=new db;
	$db->connect();
	$db->nonquery("delete from anime_theloai_fk where theloai_id=".$id);
	$db->nonquery("delete from anime_tag4 where theloai_id=".$id);
	$db->close();
 }
 
 
 function theloai_afterinsert($data,$primary,$xcrud){
	
  // $data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("insert into truyensearch(id,ten,alias) value($primary,'".mysql_real_escape_string($data->get('ten'))."','".$data->get('alias')."')");
	$db->close();*/

		//anime_theloai($primary,$data->get('theloai'));
		//Giai doan nay, copy the loai nhanh thi phai choi chieu nay
		anime_theloai($primary,$data->get('tags'));

	
	
}
 function theloai_update($data,$primary,$xcrud){
	
	//Check user --------------
	if($_SESSION['user']['level']!=1){
			//Chỉ còn lại user level 4
		$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Anime này!');
				
	}
	//-------------------------
//	echo $data->get('theloai');
//The loai dau ma khac the loai truoc thi moi goi ham ra//

	$db=new db;
	$db->connect();
	$db->query("select * from anime_theloai where id=".$primary);
	$r1=$db->fetchOne();
	if($r1['tags']!=trim($data->get('tags'))){
		//Clear tag cu da-------
		 $db->nonquery("delete from anime_tag4 where theloai_id=".$primary);
		 anime_theloai_tag($primary,trim($data->get('tags')));
	}
	

	
}

 





 function episode_remove($id){
 	$db=new db;
	$db->connect();
	
	$db->nonquery("delete from anime_tag2 where ep_id=".$id);
	$db->query("select manga from chapterk2 where id=".$id);
	if($db->num_rows()>0){
		$r1=$db->fetchOne();
		$cache =new cache;
	$cache->flush("anime".$r1['manga'],'anime');
	}
	$db->close();
 }
 function episode_update($data,$primary,$xcrud){
	
	//Check user --------------
	if($_SESSION['user']['level']!=1&& $_SESSION['user']['level']!=2&& $_SESSION['user']['level']!=5){
			//Chỉ còn lại user level 4
		$user=$data->get('user_upload');
		if($_SESSION['user']['username']!=$user){
			$xcrud->set_exception('id','Bạn Không Có Permission để cập nhật thông tin Episode này!');
			
		}
	}
	$data->set('seourl',khongdau(trim($data->get('ten'))));
	//Clear tags
		$db=new db;
		$db->connect();
		$db->query("select ten from chapterk2 where id=".$primary);
		if($db->num_rows()>0){
		
			$r1=$db->fetchOne();
			if($r1['ten']!=$data->get('ten')){
				
				 //get anime name
				 $db->query("select name from anime where id=".$data->get('manga'));
				 if($db->num_rows()>0){
				
				 	$r2=$db->fetchOne();
					//-------Remove tag cu --------
					 $db->nonquery("delete from anime_tag2_fk where ep_id=".$primary);
					  $db->nonquery("update chapterk2 set gtag=0 where id=".$primary);
					 
				
					 
					
					//  $string=tag2_generate($r2['name'],trim($data->get('name')));
					//  echo $string;
					//  anime_tag2($primary,$data->get('fansub_id'),$data->get('anime_id'),$string);
				 }
				 // 
			}
		}
	//Check name không giống cũ thì phải clear tag //
	//-------------------------
//	echo $data->get('theloai');
//	anime_theloai($primary,$data->get('theloai'));
	//$data->set('alias',khongdau(trim($data->get('name'))));
	/*$db=new db;
	$db->connect();
	$db->query("update truyensearch set ten='".mysql_real_escape_string($data->get('ten'))."',alias='".$data->get('alias')."' where id=".$primary);
   $db->close();*/
	
	//Clear cache anime
	$cache =new cache;
	$cache->flush("anime".$data->get('anime_id'),'anime');
	$sub=intval($primary)%100;
	$cache->flush('manga_get_chapter_detail'.$primary,'chapter/'.$sub);
	
	
}
 //---------------------------------End off anime function
 
?>
